﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace onPulse01
{
    public class GoogleData
    {
        #region namespaces
        static string namespaceAtom = "http://www.w3.org/2005/Atom";
        static string namespaceHealth = "urn:astm-org:CCR";
        #endregion

        #region xml elements
        XName qualifiedName = XName.Get("entry", namespaceAtom);
        XName result_Title = XName.Get("title", namespaceAtom);
        XName result_Published = XName.Get("published", namespaceAtom);
        XName result_Test = XName.Get("Test", namespaceHealth);
        XName result_DateTime = XName.Get("DateTime", namespaceHealth);
        XName result_ExactDateTime = XName.Get("ExactDateTime", namespaceHealth);
        XName result_TestResult = XName.Get("TestResult", namespaceHealth);
        XName result_Value = XName.Get("Value", namespaceHealth);
        #endregion

        #region bloodPresureType
        private enum bloodPresureType
        {
            Systolic, Diastolic
        }
        #endregion

        /// <summary>
        /// List of profiles on GoogleHealth Account
        /// </summary>
        /// <param name="xml_profile"></param>
        /// <returns></returns>
        public List<ProfileRecord> getProfilesList(string xml_profile)
        {
            XElement xml = XElement.Parse(xml_profile);

            XName qualifiedName = XName.Get("entry", namespaceAtom);
            XName record_Name = XName.Get("title", namespaceAtom);
            XName record_ProfileId = XName.Get("content", namespaceAtom);

            List<ProfileRecord> profiles = (from p in xml.Descendants(qualifiedName)
                                            select new ProfileRecord
                                            {
                                                Name = p.Element(record_Name).Value,
                                                Id = p.Element(record_ProfileId).Value
                                            }).ToList();

            return profiles;
        }
        
        /// <summary>
        /// Final BloodPresure Results 
        /// </summary>
        /// <param name="xml_results"></param>
        /// <returns></returns>
        public List<BloodPresureRecord> getBloodPresureResults(string xml_results)
        {
            List<BloodPresureRecord> results = new List<BloodPresureRecord>();

            List<Preview_BloodPresureRecord> systolic_results = getTestResult(xml_results, bloodPresureType.Systolic);
            List<Preview_BloodPresureRecord> diastolic_results = getTestResult(xml_results, bloodPresureType.Diastolic);

            for (int i = 0; i < systolic_results.Count; i++)
            {
                results.Add(
                    new BloodPresureRecord(
                        systolic_results[i].Test.Value,         //systolic value
                        diastolic_results[i].Test.Value,        //diastolic value
                        systolic_results[i].Published,          //published time (data and time)
                        systolic_results[i].Test.ExactDateTime  //exact time (only data)
                    ));
            }
            //sortowanko 
            results.Sort(delegate(BloodPresureRecord b1, BloodPresureRecord b2) { return b1.Published.CompareTo(b2.Published); });
            //najnowsze na górze ;)
            results.Reverse();
            return results;
        }       

        private List<Preview_BloodPresureRecord> getTestResult(string xml_results, bloodPresureType type)
        {
            string presureType = (type == bloodPresureType.Systolic) ? "Systolic Blood Pressure" : "Diastolic Blood Pressure";

            XElement xml = XElement.Parse(xml_results);

            var result = (from r in xml.Descendants(qualifiedName)
                          where ((string)r.Element(result_Title).Value).Equals(presureType)
                                 select new Preview_BloodPresureRecord
                                 {
                                     Published = r.Element(result_Published).Value,

                                     Test = (from t in r.Descendants(result_Test)
                                             select new TestResult
                                            {
                                                ExactDateTime = t.Elements(result_DateTime).Elements(result_ExactDateTime).Single().Value,
                                                Value = t.Elements(result_TestResult).Elements(result_Value).Single().Value
                                            }).Single()
                                 }).ToList();

            return result;
        }
        
        private class Preview_BloodPresureRecord
        {
            public string Published { get; set; }
            public TestResult Test { get; set; }
        }

        private class TestResult
        {
            public string ExactDateTime { get; set; }
            public string Value { get; set; }
        }    
    }
    

    public class BloodPresureRecord
    {
        public int Systolic { get; set; }
        public int Diastolic { get; set; }
        public DateTime Published { get; set; }
        public DateTime ExactDateTime { get; set; }

        public BloodPresureRecord(int systolic, int diastolic, string date)
        {
            Systolic = systolic;
            Diastolic = diastolic;
            Published = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            ExactDateTime = Published;
        }

        public BloodPresureRecord(int systolic, int diastolic, string published, string exactDateTime)
        {
            Systolic = systolic;
            Diastolic = diastolic;
            Published = DateTime.ParseExact(published, "yyyy-MM-dd'T'HH:mm:ss.FFF'Z'", CultureInfo.InvariantCulture);
            try
            {
                ExactDateTime = DateTime.ParseExact(exactDateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            }
            catch (FormatException)
            {
                ExactDateTime = DateTime.ParseExact(published, "yyyy-MM-dd'T'HH:mm:ss.FFF'Z'", CultureInfo.InvariantCulture);
            }
        }

        public BloodPresureRecord(string systolic, string diastolic, string published, string exactDateTime)
        {
            Systolic = Int32.Parse(systolic);
            Diastolic = Int32.Parse(diastolic);
            Published = DateTime.ParseExact(published, "yyyy-MM-dd'T'HH:mm:ss.FFF'Z'", CultureInfo.InvariantCulture);
            try
            {
                ExactDateTime = DateTime.ParseExact(exactDateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            }
            catch (FormatException)
            {
                ExactDateTime = DateTime.ParseExact(published, "yyyy-MM-dd'T'HH:mm:ss.FFF'Z'", CultureInfo.InvariantCulture);
            }
        }

        public string Day
        {
            get { return ExactDateTime.Day.ToString(); }
        }

        public string Month
        {
            get { return ExactDateTime.ToString("ddMMMM").Substring(2); }
        }
    }

    public class ProfileRecord
    {

        public string Name { get; set; }
        public string Id { get; set; }

        public ProfileRecord()
        {
        }

        public ProfileRecord(string name, string id)
        {
            Name = name;
            Id = id;
        }
    }


}
